﻿using System.Text.Json;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

var adbClient = new AdbClient("http://localhost:8001");
var ocrClient = new OcrClient("http://localhost:8003");
var matchClient = new MatchClient("http://localhost:8002");

string deviceId = "192.168.1.16";

// 连接设备
var connectResult = await adbClient.ConnectDevice(deviceId);
Console.WriteLine($"连接设备: {connectResult.success}, 消息: {connectResult.message}");

// 获取截图
var screenshotResult = await adbClient.GetScreenshot(deviceId);
if (screenshotResult.success)
{
    try
    {

        
        using var screenshot = await OcrClient.BytesToImage(screenshotResult.screenshot ?? throw new Exception());
        // // 转换为 OCR 所需的格式
        // var imageData = ImageToBase64(screenshot);
        
        var imageData = await OcrClient.ImageToBase64(screenshot);

        try
        {
            var ocrResult = await ocrClient.SendOcrRequest(imageData);
            if (ocrResult.success)
            {
                var recTexts = ((JsonElement)(ocrResult.result?["ocr_result"] ?? throw new Exception(""))).GetProperty("rec_texts");
                Console.WriteLine($"OCR 识别成功: {recTexts}");
            }
            else
            {
                Console.WriteLine($"OCR 识别失败: {ocrResult.message}");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine($"OCR 识别出现异常: {e.Message}");
        }

        // 随机截取模板图
        var templateImg = GetRandomTemplateImage(screenshot);
        var mainBase64 = ImageToBase64(screenshot);
        var templateBase64 = ImageToBase64(templateImg);

        try
        {
            var matchResult = await matchClient.MatchImagesBase64(mainBase64, templateBase64);
            if (matchResult.success)
            {
                Console.WriteLine($"图片匹配成功: {JsonSerializer.Serialize(matchResult.result)}");
            }
            else
            {
                Console.WriteLine($"图片匹配失败: {matchResult.message}");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine($"图片匹配出现异常: {e.Message}");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"处理截图时出错: {ex.Message}");
    }
}
else
{
    Console.WriteLine($"获取截图失败: {screenshotResult.message}");
}

// 断开设备连接
var (success, message) = await adbClient.DisconnectDevice(deviceId);
Console.WriteLine($"断开设备连接: {success}, 消息: {message}");

Image<Rgba32> GetRandomTemplateImage(Image<Rgba32> mainImg)
{
    var random = new Random();
    var templateWidth = random.Next(50, 201);
    var templateHeight = random.Next(50, 201);
    var x = random.Next(0, mainImg.Width - templateWidth);
    var y = random.Next(0, mainImg.Height - templateHeight);

    // 使用 ImageSharp 的 Crop 方法
    var templateImg = mainImg.Clone(ctx => ctx.Crop(new Rectangle(x, y, templateWidth, templateHeight)));
    return templateImg;
}

string ImageToBase64(Image<Rgba32> img)
{
    using var ms = new MemoryStream();
    img.SaveAsPng(ms);
    var imgBytes = ms.ToArray();
    return $"data:image/png;base64,{Convert.ToBase64String(imgBytes)}";
}
